var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submit:'',
    imgArray:[],
    content:'',
  },
    quxiao:function(event){
      wx.redirectTo({
        url: '../onlinePage/onlinePage',
      })
    },
  queding:function(event){
    var that=this
    console.log(that.data.content)
    console.log(that.data.imgArray)
    //做判空处理
    var content=that.data.content
    if(content==''){
      wx.showModal({
        title: '',
        content: '输入问题为空',
        showCancel:false,
        confirmColor:'#02a0e9',
        success:function(res){
          if(confirm){
            
                } else {
                  wx.showToast({
                    title: '发布提问失败',
                  })
                  wx.navigateBack({
                    
                  })
                }
              }
            })
          }else{
            console.log(that.data.imgArray)
            var imgStr=that.data.imgArray.join(',')
            console.log(imgStr)
      wx.request({
        url: app.globalData.baseUrl + 'onlinequestion/question',
        method: 'POST',
        data: {
          content: that.data.content,
          imgList: that.data.imgArray
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == 0) {
            wx.showToast({
              title: '发布提问成功',
            })
            wx.navigateBack({

            })
          }else{
            wx.showModal({
              title: '',
              content: '您的积分不足,是否充值？',
              success:function(res){
                if(res.confirm){
                  wx.navigateTo({
                    url: '/geren/yuePage/yuePager',
                  })
                }
              }
            })
          }
        }
      })
    }
   
      // wx.showLoading({
      //   title: '发布成功',
      // })
  },
  changeStyle:function(event){
    console.log('发布我的问题')
    console.log(event)
     this.setData({
       submit:'color:#000',
       content:event.detail.value
     })
    //  console.log(event)
     
     
  },
  chooseImage:function(event){
    var that=this
   wx.chooseImage({
     count:2,
     success: function(res) {
       console.log(res)
       var len=res.tempFilePaths.length
      
       for(var i=0;i<len;i++){
         wx.uploadFile({
           url: app.globalData.baseUrl + 'file/wechat',
           filePath: res.tempFilePaths[i],
           name: 'files',
           formData:{
            'modular':8
           },
           header:{
             'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
           },
           success:function(res){
             console.log(res)
             var imgData=JSON.parse(res.data)
             var imgArr=that.data.imgArray
             var len = imgArr.push(imgData.data)
             console.log(len)

             console.log(imgArr)
             that.setData({
               imgArray:imgArr
             })
           },
           fail:function(err){

             wx.showToast({
               icon:'none',
               title: '上传图片失败',
             })
             console.log(err)
           }
         })
        //  var it='imgArray['+i+']'
        //  that.setData({
        //    [it]:res.tempFilePaths[i]
        //  })
       }
     },
   })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: app.globalData.baseUrl+'admin/integral/integral/1019',
      method:'get',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
        wx.showModal({
          title: '',
          content: '发布问题需要'+(res.data.data.numerical*-1)+'积分',
          success:function(res){
            if(!res.confirm){
              wx.navigateBack({
                
              })
            }
          }
        })
      },
      fail:function(res){

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})